import Vue from 'vue'


Vue.prototype.$successMsg = function (message = '') {
    this.$message({
        message,
        type: 'success',
        duration: 5000
    })
}
Vue.prototype.$errorMsg = function (message = '') {
    this.$message({
        message,
        type: 'error',
        duration: 5000
    })
}
Vue.prototype.$warnMsg = function (message = '') {
    this.$message({
        message,
        type: 'warning',
        duration: 5000
    })
}
/**
 * 下载 Blob 文件函数
 * @param {Blob} blobData - Blob 数据
 * @param {Object} headers - 响应头（用于提取文件名）
 * @param {string} defaultFilename - 默认文件名
 */
export function downloadBlobFile(blobData, headers, defaultFilename = '导出文件.xlsx') {
    const contentDisposition = headers["content-disposition"];
    let filename = defaultFilename; // 默认文件名
    if (contentDisposition) {
        const utf8FilenameRegex = /filename\*=UTF-8''([\w%\-\.]+)/i;
        const asciiFilenameRegex = /filename="?([^"]+)"?/i;

        const utf8Matches = contentDisposition.match(utf8FilenameRegex);
        if (utf8Matches && utf8Matches[1]) {
            filename = decodeURIComponent(utf8Matches[1]);
        } else {
            const asciiMatches = contentDisposition.match(asciiFilenameRegex);
            if (asciiMatches && asciiMatches[1]) {
                filename = asciiMatches[1];
            }
        }
    }
    const blob = new Blob([blobData], {
        type: "application/vnd.ms-excel",
    });
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.style.display = "none";
    a.href = url;
    a.download = filename; // 下载文件名
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a); // 下载完成移除元素
    window.URL.revokeObjectURL(url); // 释放掉blob对象
}


export const encrypt = (pwd) => {
    let key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQCRRX24I2Z2Sp3aVE8uGVmRWqw0pT4jXpWhdSCd9RpfBQXj054iTY5WfZtLhztbGyJGhxXIVTx4/JR3hZsMtMONIXz2gHd2ZFBQ2WA/6ZZp3RQ4tC8nCMx0j8j0hIgo1Yw55OW9bvRtnqbKdEWAHDjbmF321s7IiEaUnU7yheAQIDAQAB"
    let encrypt = new JSEncrypt();;
    encrypt.setPublicKey(key);
    let encrypted = encrypt.encrypt(pwd);
    return encrypted;
}